$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var STANDARD = "";
    var pass = 1;

    //批号   获取 焦点
    $("input#goods_id").focus();

    //根据扫描内容 得到 信息
    $("#goods_id").on("keydown", function (event) {
        if (event.keyCode == "13") {
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            //$("#goods_id").blur();
            if (!val) {
                $alert("请扫描产品标签！");
                pass = 1;
                setTimeout(function () {
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            }
            //加载提示符 显示
            loading_show("搜索中...");
            //获取工单
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/patrol/getWarehouseForPatrol',
                data: {
                    batch: val
                },
                dataType: 'json',
                json: 'callback',
                success: function (res) {
                    pass = 1;
                    //加载提示符 隐藏
                    loading_hide();
                    if (res.success) {
                        var data = res.result;
                        $("#goods_id").val("");
                        //console.log(data);
                        $alert(val + "获取成功", 1000);
                        $(".content .number").text(data.batch);
                        $(".content .inventoryName").text(data.inventoryName);
                        $(".content .inventorySpecification").text(data.inventorySpecification);
                        $(".content .inventoryCode").text(data.inventoryCode);
                        $(".content .inTime").text(data.inTime || "");
                        $(".content .machine").text(data.machine || "空");
                        $(".content .inUser").text(data.inName || "");
                        $(".content .inResult").text(data.inResult || "");
                        $(".content .laboratoryResult").text(data.laboratoryResult || "空");
                        //
                        $(".content .ZincContentValue").text(data.laboratoryZincContentValue || "");
                        $(".content .ZincContent").text(data.laboratoryZincContent || "");
                        $(".content .ZincContentResult").text(data.laboratoryZincContentResult || "");
                        $(".content .MaxForceValue").text(data.laboratoryMaxForceValue || "");
                        $(".content .MaxForce").text(data.laboratoryMaxForce || "");
                        $(".content .MaxForceResult").text(data.laboratoryMaxForceResult || "");
                        $(".content .TensileStrengthValue").text(data.laboratoryTensileStrengthValue || "");
                        $(".content .TensileStrength").text(data.laboratoryTensileStrength || "");
                        $(".content .TensileStrengthResult").text(data.laboratoryTensileStrengthResult || "");
                        $(".content .ElongationValue").text(data.laboratoryElongationValue || "");
                        $(".content .Elongation").text(data.laboratoryElongation || "");
                        $(".content .ElongationResult").text(data.laboratoryElongationResult || "");
                        $(".content .SpecificationValue").text(data.laboratorySpecificationValue || "");
                        $(".content .Specification").text(data.laboratorySpecification || "");
                        $(".content .SpecificationResult").text(data.laboratorySpecificationResult || "");
                        $(".content .DiameterValue").text(data.laboratoryDiameterValue || "");
                        $(".content .Diameter").text(data.laboratoryDiameter || "");
                        $(".content .DiameterResult").text(data.laboratoryDiameterResult || "");
                        $(".content .ShrinkageRateValue").text(data.laboratoryShrinkageRateValue || "");
                        $(".content .ShrinkageRate").text(data.laboratoryShrinkageRate || "");
                        $(".content .ShrinkageRateResult").text(data.laboratoryShrinkageRateResult || "");
                        $(".content .ReverseRateValue").text(data.laboratoryReverseValue || "");
                        $(".content .Reverse").text(data.laboratoryReverse || "");
                        $(".content .ReverseResult").text(data.laboratoryReverseResult || "");

                        $("select.result").val("").trigger("change");
                        $("input.comment").val("");
                    } else {
                        setTimeout(function () {
                            $("#goods_id").val("").focus();
                        }, 1400);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    //加载提示符 隐藏
                    loading_hide();
                    console.log(error);
                    setTimeout(function () {
                        $("#goods_id").val("").focus();
                    }, 1400);
                    $alert(error.responseText);
                }
            });
        }
    });

    //点击 确认
    $(".foot").on("click", ".add_receive", function () {
        if (pass == 0) {
            return
        }
        pass = 0;

        //判断数据
        if (!$(".content .number").text()) {
            $alert("请扫描产品标签！");
            pass = 1;
            return;
        } else if (!$("select.result").val()) {
            $alert("请选择巡检结果！");
            pass = 1;
            return;
        }
        //获取 信息
        var number = $(".content .number").text();

        loading_show("巡检中...");
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/patrol/patrol',
            data: {
                batch: number,
                result: $("select.result").val(),
                comment: $("input.comment").val(),
            },
            dataType: "json",
            json: "callback",
            timeout: 60000,
            cache: false,
            success: function (res) {
                pass = 1;
                loading_hide();
                if (res.success) {
                    $alert(number + "巡检成功" + $("select.result").val(), 1400);
                    setTimeout(function () {
                        window.location.href = "patrol.html";
                    }, 1300);
                } else {
                    console.log(res);
                    $alert(res.message);
                }
            },
            error: function (error) {
                pass = 1;
                if (error.status == 0) {
                    loading_hide();
                    $alert("请求超时，请重新提交！");
                } else {
                    loading_hide();
                    console.log(error);
                    $alert(error.responseText);
                }
            }
        });
    });

});

